<?php

class stContestToolboxController extends stcontroller_2_1 {

    private $_smt_user_id = 0;

    public function __construct($config) {
        parent::__construct($config);
        $this->_smt_user_id = $this->_get_smt_user_id();
//        echo $this->_smt_user_id;die;
    }

    public function loadAdminCss() { // load more css custom here
        echo "<link rel='stylesheet' href='" . $this->config['backendCss'] . "stContestToolBox.css' type='text/css' media='all' />";
        echo "<link rel='stylesheet' href='" . $this->config['backendCss'] . "form.css' type='text/css' media='all' />";
        echo "<link rel='stylesheet' href='" . $this->config['backendCss'] . "colorpicker.css' type='text/css' media='all' />";
        echo "<link rel='stylesheet' href='". $this->config['backendCss']. "prettyphoto/prettyPhoto.css' type='text/css' media='all' />";
    }

    public function setConfig($config) {
        $this->config = $config;
        if ($this->actionName == "index") {
            $this->actionName = $config ['default_action'];
        }
        $this->config ['classDir'] = $this->config ['pluginDir']. 'libs/2.1/classes/';
        $this->config ['backendCss'] = $this->config ['pluginPath'] . 'css/backend/';
        $this->config ['backendScript'] = $this->config ['pluginPath'] . 'scripts/backend/';
        $this->config ['frontendCss'] = $this->config ['pluginPath'] . 'css/frontend/';
        $this->config ['frontendScript'] = $this->config ['pluginPath'] . 'scripts/frontend/';
        $this->config ['imagesUrl'] = $this->config ['pluginPath'] . 'images/';
    }

    public function loadAdminGlobalScript(){

    }

    protected function registerTabMenu() {
	        if (isset($this->config['admin_tabs'])) {
	            $user = get_current_user_id();
	            if(!user_can($user, 'read_private_pages')){
	                $this->config['disable_tabs'][] = 'setting';
	                $this->config['disable_tabs'][] = 'addOn';
	            }
	            $this->setTabMenu($this->config['admin_tabs']);
	        }
    }

    public function loadAdminScript() { // load more js custom here

        echo '<script type="text/javascript" src="' . $this->config ['backendScript'] . 'jquery.prettyPhoto.js"></script>';
        echo '<script type="text/javascript" src="' . $this->config ['backendScript'] . 'stContestToolboxCustom.js"></script>';
        echo '<script type="text/javascript" src="' . $this->config ['backendScript'] . 'jquery.colorPicker.js"></script>';
        echo '<script type="text/javascript" src="' . $this->config ['backendScript'] . 'colorpicker.js"></script>';
    }

    public function customAdminEnqueueScript() {
        parent::customAdminEnqueueScript();
    }

    public function loadCss() {
        parent::loadCss();
    }

    public function loadScript() {
        //this is function import javascript
        parent::loadScript();
    }

    public function activationPlugin() {

    }

    public function deactivationPlugin() {
        // put your deactivation code here
        global $wpdb;
    }

    public function registerView() {
        $views = array(
        );
        $this->setView($views);
    }

   public function dashboard(){
        global $wpdb,$current_user;
        $this->view->headIcon = 'icon_re';
        $this->view->headTitle1 = 'Dashboard';
    }

   public function setting(){
    	global $wpdb,$current_user;
   	$this->view->headIcon = 'icon_re';
        $this->view->headTitle1 = 'General application settings';
        if(isset($_POST['data']['noactivetext'])) {
            update_option("noactivetext_" . $this->_get_smt_user_id(), $_POST['data']['noactivetext']);
            $arg = array(
                'status' => 'success',
                'message' => __("Update setting successfully")
            );
            echo $this->functions->sendMessage(json_encode($arg));die;
        }
        $data = array(
            'noactivetext' => get_option("noactivetext_" . $this->_get_smt_user_id())
        );
        $this->view->data = $data;
    }

    public function setting_template()
    {
        $this->view->headIcon = 'icon_re';
        $this->view->headTitle1 = 'Template settings';
        
        $templateList = $this->_getAvailableTemplateList();
        $this->view->templateList = $templateList;
        
        $columns = array(
            'template_id' => __('Template ID', $this->config['plugin_name']),
            'template_name' => __('Template Name', $this->config['plugin_name'])       
        );
        register_column_headers('stcontest_st_setting_template', $columns);
    }


    public function contest_form() {
        global $wpdb, $current_user;
        $this->view->headIcon = 'icon_re';
        $this->view->headTitle1 = 'Contest editor';

        $isHavingForm = false;

        // get current contest form
        $contestID = $_GET['id'];
//
        $contest = $wpdb->get_row("select form_fields from st_contest_contest where contest_id = {$contestID}");
        if($contest->form_fields) {
            $isHavingForm = true;
            $formFields = json_decode(str_replace("\\\"", '"', $contest->form_fields));
            $this->view->form = $formFields;
            $result = (array)$formFields->element;
            $i = 1;
            foreach($result as &$r) {
                $r->element_id = $i;
                $i++;
            }

//            echo "<pre>";print_r($result);die;

            $this->view->result = $result;
        } else {
            $form = new stdClass();
            $form->form_name = 'Contest Form';
            $form->form_id = '7';
            $form->form_description = 'This is your form description. Click here to edit.';
            $form->has_captcha = 'on';
            $form->form_success_message = 'Your submission is successfully received and stored';
            $form->form_redirect = 'http://';
            $form->page_id = '';
            $form->form_email = '';
            $form->email_style = '1';
            $form->button_label = '';
            $form->title_color = '#000080';
            $form->background_color_setting = '';
            $form->text_color_setting = '#000000';
            $form->submit_button_color = '#FFFFFF';
            $form->submit_button_text_color = '#000000';
            $form->font_type = '';
            $form->title_font_size = '24';
            $form->text_font_size = '14';
            $form->submit_font_size = '14';
            $this->view->form = $form;
        }

        $this->view->isHavingForm = $isHavingForm;
        if($contestID) {
            $this->view->contestID = $contestID;
        } else {
            $contestID = 0;
            $this->view->contestID = $contestID;
        }

    }

    public function contest_save_form()
    {
//        error_reporting(E_ALL);
//        ini_set('display_errors', '1');
        $this->view->headIcon = 'icon_re';
        $this->view->headTitle1 = 'Customize form';
        global $wpdb;
        $contestID = $_REQUEST['contest_id'];
        if(!$contestID) {
//            die("need to insert to contest instead of update");
//            $contestObj = $this->loadmodel("stContest");

            $data = array(
                'name' => 'No name contest',
                'start_date' => date("Y-m-d H:i:s"),
                'end_date' => date("Y-m-d H:i:s", time() + 7 * 24 * 60 * 60),
                'smt_user_id' => $this->_smt_user_id,
                'form_fields' => stripslashes($_POST['json'])
            );

            $wpdb->insert("st_contest_contest", $data);

            $contestID = $wpdb->insert_id;

        } else {
            $wpdb->update("st_contest_contest", array("form_fields" => stripslashes($_POST['json'])), array("contest_id" => $contestID));
        }



        $this->view->contestID = $contestID;//contest_save_form
//        echo "contest id: "; echo $contestID;die;
    }

    private function _get_smt_user_id() {
        global $current_user, $wpdb;
        $query = "SELECT id FROM `wp_st_social_user` WHERE `user_id` = {$current_user->data->ID}";
        $id = $wpdb->get_var($query);
        if(!$id) {
            $id = 1;
        }
        return $id;
    }

    private function _getActiveContest()
    {
        global $wpdb;
        $sql = "select * from st_contest_contest where is_active = 1 and smt_user_id = {$this->_smt_user_id}";
        $contest = $wpdb->get_row($sql);
        return $contest;
//        echo "<pre>";
//        print_r($contest);die;
    }

    public function contest_list()
    {
        global $wpdb,$current_user;
        $this->view->headIcon = 'icon_contest';
        $this->view->headTitle1 = '[' . $this->_smt_user_id . '] All Contests';
        $contestObj = $this->loadmodel("stContest");
        // check and delete contest
        if(isset($_POST['short_action']) && ($_POST['short_action'] == "delete")) {
            $contestObj->deleteRecords($_POST['id']);
        }

        if(isset($_POST['short_action']) && ($_POST['short_action'] == "active")) {
            $ids = explode("_", $_POST['id']);
            $sql1 = "update st_contest_contest set is_active = 0 where smt_user_id = {$this->_smt_user_id};";
            $sql = "update st_contest_contest set is_active = {$ids[1]} where contest_id = {$ids[0]};";
//            echo $sql;die;
            $wpdb->query($sql1);
            $wpdb->query($sql);
//            echo $sql1. ";$sql";die;
//            echo mysql_error();die;
        }

        // get all of contests of current SMT user
        $args = array();
        $cur_page = $_REQUEST['cur_page'] ? $_REQUEST['cur_page'] : 1;
        $limit = $this->config['per_page'];
        $start = ($cur_page - 1) * $limit;

        $sort = $_REQUEST['sort'] ? $_REQUEST['sort'] : 0;
        $sort_type = $_REQUEST['sort_type'] ? $_REQUEST['sort_type'] : 0;
        if ($_REQUEST['sort']) {
            $args['order'] = array($sort, $sort_type);
        }
        $args['limit'] = array($start, $limit);

        $args['where'] = "smt_user_id = {$this->_smt_user_id}";

        $results = $contestObj->getRecords($args);
        $args['limit'] = array();
        $total = $contestObj->countRecords($args);

        $contestFunction = new stContestToolboxFunctions($this->config);
        $pagination = $contestFunction->pagination($start, $limit, $total, $cur_page, $this->actionName);

        $columns = array(
            'contest_id' => __('ID', $this->config['plugin_name']),
            'name' => __('Name', $this->config['plugin_name']),
//            'title' => __('Title', $this->config['plugin_name']),
//            'description' => __('Description', $this->config['plugin_name']),
            'created_date' => __('Created date', $this->config['plugin_name']),
            'start_date' => __('Start date', $this->config['plugin_name']),
            'end_date' => __('End date', $this->config['plugin_name']),
        );
        register_column_headers('stcontest_st_contestlist', $columns);

        //initialize variable to view
        $this->view->results = $results;
        $this->view->pagination = $pagination;
        $activeContest = $this->_getActiveContest();
        $this->view->activeContest = $activeContest;
        $this->view->smt_user_id = $this->_smt_user_id;
    }
    
    private function _getAvailableTemplateList()
    {
        // get template name list
        $templateList = array(
            'default' => 'Default',
        ); 
        
        $user = wp_get_current_user();
        $username = $user->data->user_login;
        $userconfigfile = "../contest/{$username}/config.php";
        if(file_exists($userconfigfile)) {
            $userconfig = include $userconfigfile;
            if(isset($userconfig['available_templates'])) {
                $templateList = $userconfig['available_templates'];
            }
        }
        return $templateList;
    }

    public function contest_edit()
    {
        $this->view->headIcon = 'icon_contest';
        $this->view->headTitle1 = 'Contests';
        $contestObj = $this->loadmodel("stContest");

        if(isset($_GET['id']) && !isset($_POST['id'])) {
            $_POST['id'] = $_GET['id'];
        }

        // check and delete contest
        if(isset($_POST['id'])) {
            $r = $contestObj->getRecords(array('where' => "contest_id = {$_POST['id']}"))   ;
            $this->view->result = $r[0];
        }               

        $templateList = $this->_getAvailableTemplateList();               
        
        $this->view->templateList = $templateList;

        if(isset($_POST['data'])) {

            $from = explode('.',substr($_POST['data']['start_date'],0,10));
            $from = $from[2].'-'.$from[1].'-'.$from[0].' '.substr($_POST['data']['start_date'],11).':00';
            $_POST['data']['start_date'] = $from;

            $to = explode('.',substr($_POST['data']['end_date'],0,10));
            $to = $to[2].'-'.$to[1].'-'.$to[0].' '.substr($_POST['data']['end_date'],11).':00';
            $_POST['data']['end_date'] = $to;

            $data = $_POST['data'];
            $data['created_date'] = date("Y-m-d H:i:s");
            $data['smt_user_id'] = $this->_smt_user_id;

            if(!isset($data['show_about_tab'])) {
                $data['show_about_tab'] = "off";
            }

            if(!isset($data['show_external_tab'])) {
                $data['show_external_tab'] = "off";
            }
            
            if(!isset($data['show_overlay_text'])) {
                $data['show_overlay_text'] = "off";
            }
                                    

            if(!isset($data['show_signup_tab'])) {
                $data['show_signup_tab'] = "off";
            }
            
            if(!isset($data['show_custom_tab'])) {
                $data['show_custom_tab'] = "off";
            }

            if(!isset($data['autopost_is_active'])) {
                $data['show_signup_tab'] = "off";
            }

            if(!isset($data['show_contribution_tab'])) {
                $data['show_contribution_tab'] = "off";
            }

            if(!isset($data['like_trap_is_active'])) {
                $data['like_trap_is_active'] = "off";
            }

            if($contestObj->saveRecords($data)){
                $arg = array(
                    'status' => 'success',
                    'message' => __("Contest updated", $config['plugin_name'])
                );
                echo $this->functions->sendMessage(json_encode($arg));
//                $nextAction = "contest_list";
//                if(isset($_REQUEST['nextaction']) && $_REQUEST['nextaction'] != "") {
//                    $nextAction = $_REQUEST['nextaction'];
//                }
//                echo $this->functions->sendMessage(json_encode($arg), $nextAction);
            } else {
                $arg = array(
                    'status' => 'error',
                    //'title' => 'Ad',
                    'message' => __("Can't add contest", $config['plugin_name'])
                );
                echo $this->functions->sendMessage(json_encode($arg));
            }
            die;
        }
    }



    public function contest_delete()
    {
        if(isset($_POST['id'])) {
            $arg = array(
                'status' => 'error',
                'message' => __("Can not add contest", $config['plugin_name'])
            );
            echo $this->functions->sendMessage(json_encode($arg));
            die;
        }
    }

    public function reporting_user()
    {
        $this->view->headIcon = 'icon_contest';
        $this->view->headTitle1 = 'User report';

        global $wpdb,$current_user;

        $filter = false;
        if(isset($_POST['filter'])) {
            $filter = $_POST['filter'];
        } else {
            $filter = "all";
        }
        $this->view->filter = $filter;

        $contestObj = $this->loadmodel("stContest");
        $argscontest['where'] = "smt_user_id = {$this->_smt_user_id}";

        $contests = $contestObj->getRecords($argscontest);

        $userObj = $this->loadmodel("stContestUser");
        // check and delete contest
        if(isset($_POST['short_action']) && ($_POST['short_action'] == "delete")) {
            $userObj->deleteRecords($_POST['id']);
        }

        // get all of contests of current SMT user
        $args = array();
        $cur_page = $_REQUEST['cur_page'] ? $_REQUEST['cur_page'] : 1;
        $limit = $this->config['per_page'];
        $start = ($cur_page - 1) * $limit;

        $sort = $_REQUEST['sort'] ? $_REQUEST['sort'] : 0;
        $sort_type = $_REQUEST['sort_type'] ? $_REQUEST['sort_type'] : 0;
        if ($_REQUEST['sort']) {
            if($sort == 'age') {
                $sort = "birthday";
                if($sort_type == "desc") {
                    $sort_type = "asc";
                } else {
                    $sort_type = "desc";
                }
            }
            $args['order'] = array($sort, $sort_type);
        }
        $args['limit'] = array($start, $limit);

        $args['where'] = "smt_user_id = {$this->_smt_user_id}";

        if($filter && ($filter != "all")) {
            $args['where'] .= " AND contest_id = {$filter}";
        }


//        print_r($args);die;

        $results = $userObj->getRecords($args);
        foreach($results as &$r) {
            if($r->birthday) {
                $r->age = date("Y") - intval(substr($r->birthday,0,4));
            } else {
                $r->age = "-";
            }

        }

        $args['limit'] = array();
        $total = $userObj->countRecords($args);

        $contestFunction = new stContestToolboxFunctions($this->config);
        $pagination = $contestFunction->pagination($start, $limit, $total, $cur_page, $this->actionName);

        $columns = array(
            'user_id' => __('ID', $this->config['plugin_name']),
            'name' => __('Name', $this->config['plugin_name']),
            'age' => __('Age', $this->config['plugin_name']),
            'gender' => __('Gender', $this->config['plugin_name']),
//            'facebook_user_id' => __('Facebook', $this->config['plugin_name']),
        );
        register_column_headers('stcontest_st_userlist', $columns);

        //initialize variable to view
        $this->view->results = $results;
        $this->view->pagination = $pagination;
        $this->view->contests = $contests;
        $this->view->smt_user_id = $this->_smt_user_id;

    }

    public function reporting_contest()
    {
        $this->view->headIcon = 'icon_contest';
        $this->view->headTitle1 = 'Contests Results';

        global $wpdb,$current_user;

        $contestObj = $this->loadmodel("stContest");
        // get all of contests of current SMT user
        $args = array();
        $cur_page = $_REQUEST['cur_page'] ? $_REQUEST['cur_page'] : 1;
        $limit = $this->config['per_page'];
        $start = ($cur_page - 1) * $limit;

        $sort = $_REQUEST['sort'] ? $_REQUEST['sort'] : 0;
        $sort_type = $_REQUEST['sort_type'] ? $_REQUEST['sort_type'] : 0;

        $args['limit'] = array($start, $limit);

        $args['where'] = "smt_user_id = {$this->_smt_user_id}";

        $results = $contestObj->getRecords($args);

        $args['limit'] = array();

        $total = $contestObj->countRecords($args);

        $contestFunction = new stContestToolboxFunctions($this->config);
        $pagination = $contestFunction->pagination($start, $limit, $total, $cur_page, $this->actionName);

        $columns = array(
            'contest_id' => __('ID', $this->config['plugin_name']),
            'name' => __('Name', $this->config['plugin_name']),
            'num_of_user' => __('Number of users', $this->config['plugin_name']),
            'number_of_contest_contribution' => __('Number of contributions', $this->config['plugin_name']),
            'numer_of_contest_vote' => __('Number of votes', $this->config['plugin_name']),
            'vote_per_user' => __('Votes per user', $this->config['plugin_name']),
        );
        register_column_headers('stcontest_st_reportcontest', $columns);

        // query the contest result
        $sortString = "";
        if($sort) {
            $sortString = " order by {$sort} {$sort_type} ";
        }
        $sql = "
            select ct.*,count(*) as number_of_contest_contribution, sum(num_of_like) as numer_of_contest_vote, round((sum(num_of_like)/ num_of_user),2) as vote_per_user from
            (
                    select uct.*, count(*) as num_of_user from
                            (select * from st_contest_contest where smt_user_id = {$this->_smt_user_id}) uct,
                            (select * from st_contest_user where smt_user_id = {$this->_smt_user_id}) uus
                    where uct.contest_id = uus.contest_id
                    group by uct.contest_id
            ) ct left join
            (
                    select scd.*, count(*) as num_of_like from
                            (select * from st_contest_data where smt_user_id = {$this->_smt_user_id}) scd,
                            (select * from st_contest_user_like_data where smt_user_id = {$this->_smt_user_id}) ld
                    where scd.data_id = ld.data_id
                    group by scd.data_id
            ) dt
            on ct.contest_id = dt.contest_id
            group by ct.contest_id
            {$sortString}
            limit {$start}, {$limit}
            ";
        $results = $wpdb->get_results($sql);
        //initialize variable to view
        $this->view->results = $results;
        $this->view->pagination = $pagination;
        $this->view->smt_user_id = $this->_smt_user_id;

    }
    
    public function reporting_vote()
    {
        $this->view->headIcon = 'icon_contest';
        $this->view->headTitle1 = 'Votes report';

        global $wpdb,$current_user;

        $filter = false;
        if(isset($_POST['filter'])) {
            $filter = $_POST['filter'];
        }
        $this->view->filter = $filter;
        
        $filterString = "";
        if($filter && ($filter != "all")) {
            $filterString .= " AND cd.contest_id = {$filter}";
        }
                
        $cur_page = $_REQUEST['cur_page'] ? $_REQUEST['cur_page'] : 1;
        $limit = $this->config['per_page'];
        $start = ($cur_page - 1) * $limit;
        
        $SQL = "
            select cdd.data_id, cdd.created_time, cdd.total_like, cdd.contest_name, cl.liked_time, cl.name as facebook_name, cl.birthday
            from (
            select cd.*, cc.name as contest_name from `st_contest_data` cd, st_contest_contest cc where cd.contest_id = cc.contest_id and cc.smt_user_id = {$this->_smt_user_id} {$filterString}
            ) cdd,
            (
            SELECT cu.*,liked_time, data_id FROM `st_contest_user_like_data` ul, `st_contest_user` cu WHERE ul.smt_user_id = {$this->_smt_user_id} and ul.user_id = cu.user_id
            ) cl
            where cdd.data_id = cl.data_id
            order by liked_time DESC            
        ";
            
        $limitSQL = $SQL . "limit {$start},{$limit}";
        
        $results = $wpdb->get_results($limitSQL);
        
        foreach($results as &$r) {
            if($r->birthday) {
                $r->age = date("Y") - intval(substr($r->birthday,0,4));
            } else {
                $r->age = "-";
            }

        }
//        echo "<pre>";print_r($results);die;
        
        // get total records
        $resultsTotal = $wpdb->get_results($SQL);
        $total = count($resultsTotal);
        
        $contestFunction = new stContestToolboxFunctions($this->config);
        $pagination = $contestFunction->pagination($start, $limit, $total, $cur_page, $this->actionName);

        $columns = array(
            'facebook_name' => __('Voters FB name', $this->config['plugin_name']),
            'age' => __('Voters age', $this->config['plugin_name']),
            'liked_time' => __('Date/time for vote', $this->config['plugin_name']),
            'contest_name' => __('Name of contest the vote is for', $this->config['plugin_name']),
        );
        register_column_headers('stcontest_st_userlist', $columns);

        // get contest list
        $contestObj = $this->loadmodel("stContest");
        $argscontest['where'] = "smt_user_id = {$this->_smt_user_id}";

        $contests = $contestObj->getRecords($argscontest);
        
        //initialize variable to view
        $this->view->results = $results;
        $this->view->pagination = $pagination;
        $this->view->contests = $contests;
        $this->view->smt_user_id = $this->_smt_user_id;
    }

    public function reporting_contest_detail()
    {
        global $wpdb,$current_user;
        $this->view->headIcon = 'icon_contest';
        $this->view->headTitle1 = "Detailed statisitcs for";

        $contestID = 0;
        if(isset($_POST['short_action']) && ($_POST['short_action'] == "idcontest")){
            if(isset($_REQUEST['id'])) {
                $contestID = $_REQUEST['id'];
            }
        } else {
            if(isset($_REQUEST['contest_id'])) {
                $contestID = $_REQUEST['contest_id'];
            }
        }

        $contestDataObj = $this->loadmodel("stContestData");
        $contestObj = $this->loadmodel("stContest");

        $contest = $contestObj->getByID($contestID);

        $this->view->headTitle1 = "Detailed statisitcs for {$contest->name}";

        // check and delete contest
        if(isset($_POST['short_action']) && ($_POST['short_action'] == "delete")) {
            $contestDataObj->deleteRecords($_POST['id']);
        }

        // get all of contests of current SMT user
        $args = array();
        $cur_page = $_REQUEST['cur_page'] ? $_REQUEST['cur_page'] : 1;
        $limit = $this->config['per_page'];
        $start = ($cur_page - 1) * $limit;

        $sort = $_REQUEST['sort'] ? $_REQUEST['sort'] : 0;
        $sort_type = $_REQUEST['sort_type'] ? $_REQUEST['sort_type'] : 0;

        $args['limit'] = array($start, $limit);

        $args['where'] = "contest_id = {$contestID}";

        $results = $contestDataObj->getRecords($args);

        $args['limit'] = array();

        $total = $contestDataObj->countRecords($args);

        $contestFunction = new stContestToolboxFunctions($this->config);
        $pagination = $contestFunction->pagination($start, $limit, $total, $cur_page, $this->actionName);

        $columns = array(
            'data_id' => __('ID', $this->config['plugin_name']),
            'name' => __('Name', $this->config['plugin_name']),
            'created_time' => __('Date/time for the contribution', $this->config['plugin_name']),
            'num_of_like' => __('Number of votes', $this->config['plugin_name']),
        );
        register_column_headers('stcontest_st_reportcontest_data', $columns);

        // query the contest result
        $sortString = "";
        if($sort) {
            $sortString = " order by {$sort} {$sort_type} ";
        }
        $sql = "
            select * from
            (
            select scd.*, ld.user_like_id, count(*) as num_of_like from
                    (select * from st_contest_data where contest_id = {$contestID}) scd
                        left join
                    (select * from st_contest_user_like_data where smt_user_id = {$this->_smt_user_id}) ld
                        on scd.data_id = ld.data_id
                    group by scd.data_id
            ) tb1 left join st_contest_user lu
            on tb1.user_id = lu.user_id
            {$sortString}
            limit {$start}, {$limit}
            ";
//        echo $sql;
        $results = $wpdb->get_results($sql);
        //initialize variable to view
        $this->view->results = $results;
        $this->view->pagination = $pagination;
        $this->view->smt_user_id = $this->_smt_user_id;
        $this->view->contest_id = $contestID;
        $this->view->contest = $contest;
    }

    public function data_show_vote()
    {
        $this->view->headIcon = 'icon_contest';
        $this->view->headTitle1 = 'User votes';

        global $wpdb,$current_user;

        $id = $_REQUEST['id'];

        // get all of contests of current SMT user
        $args = array();
        $cur_page = $_REQUEST['cur_page'] ? $_REQUEST['cur_page'] : 1;
        $limit = $this->config['per_page'];
        $start = ($cur_page - 1) * $limit;

        $sort = $_REQUEST['sort'] ? $_REQUEST['sort'] : 0;
        $sort_type = $_REQUEST['sort_type'] ? $_REQUEST['sort_type'] : 0;

        $sortString = "";
        if($sort) {
            $sortString = " order by {$sort} {$sort_type} ";
        }
//        print_r($args);die;
        $sql = "select *, (year(now())- year(birthday)) as age from
                (
                        select * from st_contest_user_like_data where data_id = {$id}
                ) l
                left join
                st_contest_user u
                on l.user_id = u.user_id
                 {$sortString}
                 limit {$start}, {$limit}
                ";
//         echo $sql;die;
        $totalSQL = "select count(*) as total from st_contest_user_like_data where data_id = {$id}";
        $results = $wpdb->get_results($sql);

        $row = $wpdb->get_row($totalSQL);

        $total = $row->total;

        $contestFunction = new stContestToolboxFunctions($this->config);
        $pagination = $contestFunction->pagination($start, $limit, $total, $cur_page, $this->actionName);

        $columns = array(
            'user_id' => __('ID', $this->config['plugin_name']),
            'name' => __('Name', $this->config['plugin_name']),
            'age' => __('Age', $this->config['plugin_name']),
            'liked_time' => __('Date/time for vote', $this->config['plugin_name']),
        );
        register_column_headers('stcontest_st_uservote', $columns);

        //initialize variable to view
        $this->view->results = $results;
        $this->view->pagination = $pagination;
        $this->view->smt_user_id = $this->_smt_user_id;
        $this->view->data_id = $id;
    }
}

?>